package com.example.wan1.adapter;

import android.os.Build;
import android.text.Html;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.wan1.R;
import com.example.wan1.constant.Constants;
import com.example.wan1.model.TodoBean;

import java.util.Date;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

/***
 * 创建时间：2020/7/31 18:47
 * 创建人：10850
 * 功能描述：
 */
public class TodoAdapter extends BaseQuickAdapter<TodoBean, BaseViewHolder> {

    public TodoAdapter(int layoutResId, @Nullable List<TodoBean> data) {
        super(layoutResId, data);
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void convert(@NonNull BaseViewHolder holder, TodoBean todoBean) {
        holder.setText(R.id.item_todo_title,
                Html.fromHtml(todoBean.getTitle(), Html.FROM_HTML_MODE_COMPACT));
        holder.setText(R.id.item_todo_content,
                Html.fromHtml(todoBean.getContent(), Html.FROM_HTML_MODE_COMPACT));

        holder.setText(R.id.item_todo_date,
                Html.fromHtml(todoBean.getDateStr(), Html.FROM_HTML_MODE_COMPACT));

        if (todoBean.getStatus() == 1) {
            //完成
            holder.setImageResource(R.id.item_todo_status, R.drawable.todo_done);
            holder.setVisible(R.id.item_todo_status, true);
            holder.getView(R.id.item_todo_cardview).setForeground(mContext.getDrawable(R.drawable.todo_foreground));
        } else {
            //未完成，根据时间判断需不需要加前景
            holder.setImageResource(R.id.item_todo_status, R.drawable.todo_not_done);
            if (todoBean.getDate() < new Date().getTime()) {
                holder.setVisible(R.id.item_todo_status, true);
                holder.getView(R.id.item_todo_cardview).setForeground(mContext.getDrawable(R.drawable.todo_foreground));
            } else {
                holder.setVisible(R.id.item_todo_status, false);
                holder.getView(R.id.item_todo_cardview).setForeground(null);
            }
        }

        if (todoBean.getPriority() == Constants.TODO_IMPORTANT) {
            holder.setText(R.id.item_todo_priority, R.string.todo_piority_important);
        } else {
            holder.setText(R.id.item_todo_priority, R.string.todo_piority_normal);
        }

        if (todoBean.getType() == Constants.TODO_WORK_TYPE) {
            holder.setText(R.id.item_todo_type, R.string.todo_type_work);
        } else if (todoBean.getType() == Constants.TODO_STUDY_TYPE) {
            holder.setText(R.id.item_todo_type, R.string.todo_type_study);
        } else {
            holder.setText(R.id.item_todo_type, R.string.todo_type_play);
        }

        holder.addOnClickListener(R.id.item_todo_more);
    }
}
